﻿<?php 

function connectDB(){ //ket noi csdl
	mysql_query("set names 'utf8'");
	mysql_connect('localhost','root','') or die('Không thể kết nối tới máy chủ!');
	mysql_select_db('qlcf') or die('Không tìm thấy cơ sở dữ liệu!');
	mysql_query("set names 'utf8'");
}

function checkLogin($user, $pass){   //kiem tra dang nhap
$sql="SELECT * FROM taikhoan WHERE MATK='$user' AND MATKHAU='$pass'";
		 $rs= mysql_query($sql);
		 if($rw= mysql_fetch_array($rs)){
		 return  true;    
		   }else{
				 return false;
		 }
};

function getMon(){ // lay danh sach cac mon
	if (isset($_POST['getMon'])){
		$id=$_POST['getMon'];
		if( $id == 'all') $sql= "SELECT * FROM mon"; // lay tat ca mon
			else $sql= "SELECT * FROM mon WHERE mamon='$id'";// lay 1 mon theo id
		$rs= mysql_query($sql);
		$i=-1;
		while($rw= mysql_fetch_array($rs)){ // vong lap de lay tat ca ca mon 
			$i++;	
			$mon[$i] = array(  //tao mang cac mon	
		 	   'MAMON' => $rw['MAMON']
			  ,'MANHOM' => $rw['MANHOM']
			  ,'TENMON' => $rw['TENMON']
			  ,'DONGIA' => $rw['DONGIA']
			  ,'DVT' => $rw['DVT']);	
		};	
		echo json_encode($mon);  //dung ham json_encode de chuyen mang $member thanh chuoi JSON  
	   die; //ket thuc tra ve du lieu va stop khong cho chay tiep
	};
};
function getNhom(){ // lay danh sach cac nhom
	if (isset($_POST['getNhom'])){
		$id=$_POST['getNhom'];
		if( $id == 'all') $sql= "SELECT * FROM nhom"; // lay tat ca nhom
			else $sql= "SELECT * FROM nhom WHERE manhom='$id'";// lay 1 nhom theo id
		$rs= mysql_query($sql);
		$i=-1;
		while($rw= mysql_fetch_array($rs)){ // vong lap de lay tat ca ca nhom
			$i++;	
			$nhom[$i] = array(  //tao mang cac nhom
			   'MANHOM' => $rw['MANHOM']
			  ,'TENNHOM' => $rw['TENNHOM']);	
		};	
		echo json_encode($nhom);  //dung ham json_encode de chuyen mang $mhom thanh chuoi JSON  
	   die; //ket thuc tra ve du lieu va stop khong cho chay tiep
	};
};
function getKhuVuc(){ // lay danh sach cac khu vuc
	if (isset($_POST['getKhuVuc'])){
		$id=$_POST['getKhuVuc'];
		if( $id == 'all') $sql= "SELECT * FROM khuvuc"; // lay tat ca khu vuc
			else $sql= "SELECT * FROM khuvuc WHERE makv='$id'";// lay 1khu vuc theo id
		$rs= mysql_query($sql);
		$i=-1;
		while($rw= mysql_fetch_array($rs)){ // vong lap de lay tat ca ca khu vuc
			$i++;	
			$kv[$i] = array(  //tao mang cac khu vuc
			   'MAKV' => $rw['MAKV']
			  ,'TENKV' => $rw['TENKV']);	
		};	
		echo json_encode($kv);  //dung ham json_encode de chuyen mang $kv thanh chuoi JSON  
	   die; //ket thuc tra ve du lieu va stop khong cho chay tiep
	};
};

function getKhuvucAndBan(){ // lay danh sach cac khu vuc
	if (isset($_POST['getKhuvucAndBan'])){
		$id=$_POST['getKhuvucAndBan'];
		if( $id == 'all') $sql= "SELECT * FROM khuvuc k , ban b  WHERE k.makv=b.makv";
			else $sql= "SELECT * FROM khuvuc k , ban b  WHERE makv='$id' and k.makv=b.makv";// lay 1khu vuc theo id
		$rs= mysql_query($sql);
		$i=-1;
		while($rw= mysql_fetch_array($rs)){ // vong lap de lay tat ca ca khu vuc
			$i++;	
				

			$kv[$i] = array( 
			  	'makv'=> $rw['MAKV'],
			  	'tenkv'=> $rw['TENKV'],
			  	'ban' => array( 
				   'soban' => $rw['SOBAN']
				  ,'soghe' => $rw['SL_GHE'])
			  );
		};	
		echo json_encode($kv);  //dung ham json_encode de chuyen mang $kv thanh chuoi JSON  
	   die; //ket thuc tra ve du lieu va stop khong cho chay tiep
	};
};





connectDB();
checkLogin('1','1');
getMon();
getNhom();
getKhuVuc();
getKhuvucAndBan();
?>